package com.villaday.idf.custom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.model.SmsEntity;
import com.villaday.idf.model.vipsGroupsEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.ToastUtil;


/**
 * 设置分组 对话框
 */
@SuppressLint("ValidFragment")
public class SettingEnvelopePerentDialog extends BaseDialog implements View.OnClickListener, OkHttpUtil.OnDataListener, TextWatcher {
    TextView tvDisplayRabit;
    EditText etEnvelopeRabit; //设置该组分佣比例
    Context context;
    int type;


    private OnSetEnvelopListener mListener;

    private Gson gson = new GsonBuilder().create();


    public static SettingEnvelopePerentDialog newInstance(Context context, int type, String percent) {
        SettingEnvelopePerentDialog dialog = new SettingEnvelopePerentDialog(context);
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("percent", percent);
        dialog.setArguments(bundle);
        return dialog;
    }

    public SettingEnvelopePerentDialog(Context context) {
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_setting_envelope_percent, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(true);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
//        window.setWindowAnimations(R.style.animTranslateTop);


        tvDisplayRabit = (TextView) view.findViewById(R.id.tv_rabit);
        etEnvelopeRabit = (EditText) view.findViewById(R.id.et_rabit);
        etEnvelopeRabit.addTextChangedListener(textWatcher);
        TextView btnCancel = (TextView) view.findViewById(R.id.tv_cancel_btn);
        TextView btnSaveGroup = (TextView) view.findViewById(R.id.tv_save_group);
        type = getArguments().getInt("type");
        if (!TextUtils.isEmpty(getArguments().getString("percent"))) {
            etEnvelopeRabit.setText(getArguments().getString("percent"));
            etEnvelopeRabit.setSelection(etEnvelopeRabit.getText().toString().length());
        }
        btnCancel.setOnClickListener(this);
        btnSaveGroup.setOnClickListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    public void setListener(OnSetEnvelopListener icListener) {
        this.mListener = icListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_save_group: {  //保存红包比例

                if (mListener != null) {

                    if (TextUtils.isEmpty(etEnvelopeRabit.getText().toString())) {
                        ToastUtil.initToast(context, "请设置红包比例");
                    } else if (!(etEnvelopeRabit.getText().toString().matches(Constant.Strings.RegexInt) || etEnvelopeRabit.getText().toString().matches(Constant.Strings.RegexFloat))) {
                        ToastUtil.initToast(context, "输入红包比例数字样式不正确");
                    } else if (Float.parseFloat(etEnvelopeRabit.getText().toString()) < 0) {
                        ToastUtil.initToast(context, "输入红包比例不能小于0");
                    } else if (Float.parseFloat(etEnvelopeRabit.getText().toString()) > 100) {
                        ToastUtil.initToast(context, "输入红包比例不能大于100");
                    } else {
                        mListener.crateEnvelope(etEnvelopeRabit.getText().toString(), type);
                        dismiss();
                    }
                }
            }

            break;

            case R.id.tv_cancel_btn: {  //取消分组
                if (mListener != null) {
                    mListener.cancelEnvelope();
                }

                dismiss();
            }
            break;
        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            if (Constant.URL.sms.equals(url)) { //刷新Token
                LogeUtil.e("sms", json);
                SmsEntity se = gson.fromJson(json, SmsEntity.class);
                if (Constant.Strings.SUC.equals(se.getCode())) {
                    ToastUtil.initToast(context, se.getMsg());
                } else {
                    ToastUtil.initToast(context, se.getMsg());
                }
            }

        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    public interface OnSetEnvelopListener {
        void crateEnvelope(String groupRabit, int type);

        void cancelEnvelope();
    }

    /**
     * 输入保留两位
     */
    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (s.toString().contains(".")) {
                if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                    s = s.toString().subSequence(0,
                            s.toString().indexOf(".") + 3);
                    etEnvelopeRabit.setText(s);
                    etEnvelopeRabit.setSelection(s.length());
                }
            }
            if (s.toString().trim().substring(0).equals(".")) {
                s = "0" + s;
                etEnvelopeRabit.setText(s);
                etEnvelopeRabit.setSelection(2);
            }
            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    etEnvelopeRabit.setText(s.subSequence(0, 1));
                    etEnvelopeRabit.setSelection(1);
                    return;
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };
}
